<?php


namespace libs;


trait Mysql
{
    private $host;// 地址
    private $port;//端口
    private $user;//用户名
    private $pass;// 密码
    private $dbname;//库名
    private $link;

    public function __construct(array $arr = array())
    {
       
        $this->host = $arr['host']??'127.0.0.1';
        $this->port = $arr['port']??'3306';
        $this->user = $arr['user']??'root';
        $this->pass = $arr['pass']??'root';
        $this->dbname = $arr['dbname']??'caiji_xitong';
        $this->connect();
    }

    /**
     * 连接数据库
     */
    private function connect()
    {
        $this->link = mysqli_connect($this->host, $this->user, $this->pass, $this->dbname, $this->port);
        if (!$this->link) {
            echo "Error: Unable to connect to MySQL." . PHP_EOL;
            echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
            echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
            exit;
        }
    }


    /*
      * 增加数据
      * @param1 string $sql，要执行的插入语句
      * @return boolean，成功返回是自动增长的ID，失败返回FALSE
     */
    public function insert($sql)
    {
        //发送数据
        $this->db_query($sql);
        //成功返回自增ID
        return mysqli_affected_rows($this->link) ? mysqli_insert_id($this->link) : FALSE;
    }

    /*
     * 删除数据
     * @param1 string $sql，要执行的删除语句
     * @return Boolean，成功返回受影响的行数，失败返回FALSE
    */
    public function delete($sql)
    {
        //发送SQL
        $this->db_query($sql);
        //判断结果
        return mysqli_affected_rows($this->link) ? mysqli_affected_rows($this->link) : FALSE;
    }

    /*
     * 更新数据
     * @param1 string $sql，要执行的更新语句
     * @return Boolean，成功返回受影响的行数，失败返回FALSE
    */
    public function update($sql)
    {
        //发送SQL
        $this->db_query($sql);
        //判断结果
        return mysqli_affected_rows($this->link) ? mysqli_affected_rows($this->link) : FALSE;
    }

    /**
     * 查询一条数据
     * @return mixed
     */
    public function getRow($sql)
    {
        $res = $this->db_query($sql);
        return mysqli_num_rows($res) ? mysqli_fetch_assoc($res) : FALSE;
    }

    /**
     * 查询多条数据
     * @return mixed
     */
    public function getAll($sql)
    {
        $res = $this->db_query($sql);
        //判断返回
        if (mysqli_num_rows($res)) {
            //循环遍历
            $list = array();
            //遍历
            while ($row = mysqli_fetch_assoc($res)) {
                $list[] = $row;
            }
            //返回
            return $list;
        }
        //返回FALSE
        return FALSE;
    }


    /**
     * 查询数据接口
     * @return mixed
     */
    private function db_query($sql)
    {
        $res = mysqli_query($this->link, $sql);
        if (!$res) {
            //结果出错了
            //暴力处理，如果是真实线上项目（生产环境）必须写入到日志文件
            echo '语句出现错误：<br/>';
            echo '错误编号' . mysqli_error($this->link) . '<br/>';
            echo '错误内容' . mysqli_error($this->link) . '<br/>';
            echo 'sql语句' . $sql . '<br/>';
            exit;
        }
        //没有错误
        return $res;
    }
}